package order

import (
	"context"
	"fmt"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/order"
	orderReq "github.com/flipped-aurora/gin-vue-admin/server/model/order/request"
	RabbitMQ "github.com/flipped-aurora/gin-vue-admin/server/pkg"
)

type OrderService struct{}

// CreateOrder 创建订单管理记录
// Author [yourname](https://github.com/yourname)
func (ordersService *OrderService) CreateOrder(ctx context.Context, orders *order.Order) (err error) {
	err = global.GVA_DB.Create(orders).Error
	return err
}

// DeleteOrder 删除订单管理记录
// Author [yourname](https://github.com/yourname)
func (ordersService *OrderService) DeleteOrder(ctx context.Context, orderId string) (err error) {
	err = global.GVA_DB.Delete(&order.Order{}, "order_id = ?", orderId).Error
	return err
}

// DeleteOrderByIds 批量删除订单管理记录
// Author [yourname](https://github.com/yourname)
func (ordersService *OrderService) DeleteOrderByIds(ctx context.Context, orderIds []string) (err error) {
	err = global.GVA_DB.Delete(&[]order.Order{}, "order_id in ?", orderIds).Error
	return err
}

// UpdateOrder 更新订单管理记录
// Author [yourname](https://github.com/yourname)
func (ordersService *OrderService) UpdateOrder(ctx context.Context, orders order.Order) (err error) {
	err = global.GVA_DB.Model(&order.Order{}).Where("order_id = ?", orders.OrderId).Updates(&orders).Error
	key := fmt.Sprintf("%d:tips", *orders.OrderId)
	rab := RabbitMQ.NewRabbitMQSimple(key)
	var status string
	switch *orders.OrderStatus {
	case "0":
		{
			status = "未付款"
		}
	case "1":
		{
			status = "已付款"
		}
	case "2":
		{
			status = "已发货"
		}
	case "3":
		{
			status = "已收货"
		}
	case "4":
		{
			status = "已完成"
		}
	}
	data := fmt.Sprintf("您的订单:%d%s，请注意查看", *orders.UserId, status)
	rab.PublishSimple(data)
	return err
}

// GetOrder 根据orderId获取订单管理记录
// Author [yourname](https://github.com/yourname)
func (ordersService *OrderService) GetOrder(ctx context.Context, orderId string) (orders order.Order, err error) {
	err = global.GVA_DB.Where("order_id = ?", orderId).First(&orders).Error
	return
}

// GetOrderInfoList 分页获取订单管理记录
// Author [yourname](https://github.com/yourname)
func (ordersService *OrderService) GetOrderInfoList(ctx context.Context, info orderReq.OrderSearch) (list []order.Order, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&order.Order{})
	var orderss []order.Order
	// 如果有条件搜索 下方会自动创建搜索语句

	if info.UserId != nil {
		db = db.Where("user_id = ?", *info.UserId)
	}
	if info.ProductId != nil {
		db = db.Where("product_id = ?", *info.ProductId)
	}
	if info.OrderType != nil && *info.OrderType != "" {
		db = db.Where("order_type = ?", *info.OrderType)
	}
	if info.OrderStatus != nil && *info.OrderStatus != "" {
		db = db.Where("order_status = ?", *info.OrderStatus)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&orderss).Error
	return orderss, total, err
}
func (ordersService *OrderService) GetOrderPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
